package com.cug.nageoffer.user.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.cug.nageoffer.model.entity.Post;
import com.cug.nageoffer.model.vo.UserVo;
import jakarta.annotation.Resource;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ElasticSearchUtil {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 创建索引库
     */
    public void createIndex(String index,String mapping) throws IOException {
        System.out.println(restHighLevelClient);
        CreateIndexRequest request=new CreateIndexRequest(index);
        request.source(mapping, XContentType.JSON);
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

    /**
     * 添加文档
     */
    public void addDocument(String id,String index,Post post) throws IOException {
        IndexRequest request = new IndexRequest(index).id(id);
        request.source(JSONUtil.toJsonStr(post),XContentType.JSON);
        restHighLevelClient.index(request,RequestOptions.DEFAULT);
    }

    /**
     * 查询文档
     */
    public Post getDocumentById(Integer id,String index) throws IOException {
        GetRequest request = new GetRequest(index,id.toString());
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        return JSONObject.parseObject(json, Post.class);
    }

    /**
     * 全文检索文档
     */
    public List<Post> getDocumentByMatch(String index,String column,String value) throws IOException {
        SearchRequest request = new SearchRequest(index);
        request.source().query(QueryBuilders.matchQuery(column,value));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到"+total+"条数据");
        List<Post> postList=new ArrayList<>();
        for (SearchHit hit : searchHits) {
            String json = hit.getSourceAsString();
            postList.add(JSONObject.parseObject(json, Post.class));
        }
        return postList;
    }

    /**
     * 检索用户
     */
    public List<UserVo> getUserByMatch(String index, String column, String value) throws IOException {
        SearchRequest request = new SearchRequest(index);
        request.source().query(QueryBuilders.matchQuery(column,value));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到"+total+"条数据");
        List<UserVo> userList=new ArrayList<>();
        for (SearchHit hit : searchHits) {
            String json = hit.getSourceAsString();
            userList.add(JSONObject.parseObject(json, UserVo.class));
        }
        return userList;
    }
}
